package com.china08.yxyapi.schedule.res;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import com.china08.yxyapi.entity.db.res.YxyResResource;
import com.china08.yxyapi.repository.db.YxyResResourceRepository;
import com.china08.yxyapi.schedule.statistics.ResourceReportTask;

@Component
@EnableScheduling
public class ResourceRetryConvert {

    private static final Logger LOGGER = LoggerFactory.getLogger(ResourceReportTask.class);

    @Autowired
    private YxyResResourceRepository yxyResResourceRepository;

    // 一小时执行一次
    @Scheduled(fixedRate = 1 * 60 * 60 * 1000)
    public void execute() {
        LOGGER.info("查询转换失败的资源");
        List<YxyResResource> list = yxyResResourceRepository.findTop10ByConvertStatusAndConvertRetryTimesLessThan(3, 6);
        LOGGER.info("查询转换失败的资源,一共：" + list.size() + "条");
        for (YxyResResource resource : list) {
            resource.setConvertRetryTimes(resource.getConvertRetryTimes() + 1);
            resource.setConvertStatus(0);
            yxyResResourceRepository.save(resource);
        }

        LOGGER.info("查询转换失败的资源，更新完毕");
    }

}
